package com.example.demo.config;

/**
 * @author long
 * @version 1.0.0
 * @ClassName P6spySqlFormatConfig
 * @Description TODO
 * @createTime 2022.02.22 12:08
 */

import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 自定义 p6spy sql输出格式
 *
 * @author long
 */
public class P6spySqlFormatConfig implements MessageFormattingStrategy {

    public DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
        return StringUtils.isNotBlank(sql) ? dateTimeFormatter.format(LocalDateTime.now())
                + " | 耗时 " + elapsed + " ms | SQL 语句：" + StringUtils.LF + sql.replaceAll("[\\s]+", StringUtils.SPACE) + ";" : "";
    }
}
